#include <QDir>
#include "pig.h"
#include "ui_pig.h"
#include "data.h"

Pig::Pig(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Pig)
{
    ui->setupUi(this);
    vctLayout = new QVBoxLayout;
    vctLayout->setSpacing(0);
    ui->widget->setLayout(vctLayout);
}

void Pig::Init()
{
    using namespace eintik::presentation::controller;
    using namespace eintik::presentation::panel;

    // (1) new views
    control_panel = std::make_shared<ControlPanel>();
    controller = std::make_shared<GenericController>();

    // (2) add views to ui framework
    ui->layout_focal->addLayout(control_panel->focalLawLayout(), 0, 0);
    ui->layout_network->addLayout(control_panel->networkLayout(), 0, 0);
    ui->layout_device->addLayout(control_panel->controlLayout(), 0, 0);

    // (3) connect signals and slots to controller
    QObject::connect(control_panel->startButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnStartClicked);

    QObject::connect(control_panel->stopButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnStopClicked);

    QObject::connect(control_panel->connectButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnConnectClicked);

    QObject::connect(control_panel->closeButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnCloseClicked);

    QObject::connect(control_panel->setButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnSetClicked);

    QObject::connect(control_panel->configButton(), &QPushButton::clicked, controller.get(),
                     &GenericController::OnConfigClicked);

    QObject::connect(control_panel.get(), &ControlPanel::ParameterChanged, controller.get(),
                     &GenericController::OnParameterChanged);

    QObject::connect(controller.get(), &GenericController::GetParameters, control_panel.get(),
                     &ControlPanel::OnCollectValues);

    pd = std::make_shared<PigViewData>();
    QWidget *topRow = new QWidget;
    QHBoxLayout *topLayout = new QHBoxLayout(topRow);
    topLayout->setContentsMargins(0, 0, 0, 0);
    topLayout->addWidget(pd->getAScan());
    topLayout->addWidget(pd->getSScan());

    vctLayout->addWidget(topRow);
    vctLayout->addWidget(pd->getBCScan());
    vctLayout->addWidget(pd->getACScan());
}

Pig::~Pig()
{
    delete ui;
}
